'''
Created on 31.05.2013

@author: andi
'''

from dame.Controller import Controller
from dame.Interfaces import Player
from dame.Interfaces import Move
from dame.Game import CheckersBoard

class GameManager:
    
    playerToMove = 0
    def setNewGame(self, firstPlayer, secondPlayer, thirdPlayer):
        self.firstPlayer = firstPlayer
        self.secondPlayer = secondPlayer
        self.thirdPlayer = thirdPlayer
        #self.firstPlayer.startGame()
        self.firstPlayer.setFirstPlayer()
        self.secondPlayer.setSecondPlayer()
        self.thirdPlayer.setThirdPlayer()
        self.PlayerToMove = 1
        
    def play(self):

        maxMoves = 2
        move = 0
        
        self.thirdPlayer._currentGamestate.printOut()

        while not self.thirdPlayer.isTerminalState() and move < maxMoves:
            
            if (self.thirdPlayer.firstPlayerToMove()):
                mv = self.firstPlayer.getNextMove()
                
                if self.thirdPlayer.isLegalMove(mv):
                    self.playerToMove = 2
                else:
                    #break
                    continue
            else:
                mv = self.secondPlayer.getNextMove()
                
                if self.thirdPlayer.isLegalMove(mv):
                    self.playerToMove = 1
                else:
                    #break
                    continue
                
            move += 1
            self.firstPlayer.doMove(mv)
            self.secondPlayer.doMove(mv)
            self.thirdPlayer.doMove(mv)
            
            print("move ", mv)
            self.thirdPlayer._currentGamestate.printOut()
        
        print("first history: ", len(self.firstPlayer._currentGamestate.getMoveHistory()))
                
    def getWinner(self):

        if self.thirdPlayer.isTerminalState():
            if self.thirdPlayer.firstPlayerToWin():
                return 1
            else:
                return 2            
            
"""
***************************************************************************************************
CheckersPlayer
***************************************************************************************************
"""

class CheckersPlayer(Player):
    
    _isFirstPlayer = False
    _isSecondPlayer = False
    _isThirdPlayer = False
    _currentGamestate = 0;
    _hasUI = False;
    _boardUI = None
    _controller = Controller()
    
    def setStartState(self):
        self._currentGamestate = CheckersBoard()

    def setFirstPlayer(self):
        self.setStartState()
        self._isFirstPlayer = True
        self._hasUI = False
        #self._boardUI = DameUI()       
        #self._boardUI.paintAll(self._currentGamestate._board) 

    def setSecondPlayer(self):
        self.setStartState()
        self._isSecondPlayer = True

    def setThirdPlayer(self):
        self.setStartState()
        self._isThirdPlayer = True

    def getNextMove(self):        
        
        if self._isFirstPlayer:
            spieler = 1
        elif self._isSecondPlayer:
            spieler = 2
        
        if self._hasUI == True:
            pass
            #move = self._boardUI.getZug(spieler)
        else:
            
            return self._controller.getNextMove(self._currentGamestate)
            
            var = input("Zug fuer Spieler " + str(spieler) +  " eingeben: ")
            xweg = -1
            yweg = -1
            if len(var) ==11:
                xweg = int(var[8])
                yweg = int(var[10])
                return Move(spieler, int(var[0]), int(var[2]), int(var[4]), int(var[6]), xweg, yweg)
    
#             if len(var) == 6:
#                 xweg = var[4]
#                 yweg = var[5]
#             move = Move(spieler, var[0], var[1], var[2], var[3], xweg, yweg)                
#         return move
        # Hier wrde das ganze min/max dings kommen nach mir
#        Vom curentGamestate die mglichen Zge holen

    def isLegalMove(self, mv):
        return self._currentGamestate.possibleMove(mv)
    
    def doMove(self, mv):
        self._currentGamestate.doMove(mv)
            
        if self._hasUI == True:
            pass
            #self._boardUI.paintAll(self._currentGamestate._board)
        
    def isTerminalState(self):
        return False
    #    Fertig ist wenn: der Spieler nicht mehr bewegen kann

    def firstPlayerToMove(self):
        # Der erste Spieler ist weiss
        return self._currentGamestate._playerToMove == CheckersBoard._white

    def secondPlayerToMove(self):
        # Der zweite Spieler ist Schwarz
        return self._currentGamestate._playerToMove == CheckersBoard._black
    
    def firstPlayerToWin(self):
        raise NotImplementedError("You should have implemented this")    

    def secondPlayerToWin(self):
        raise NotImplementedError("You should have implemented this")    

    def showGamestate(self):
        raise NotImplementedError("You should have implemented this")    
    

game = GameManager()
p1 = CheckersPlayer()
p2 = CheckersPlayer()
p3 = CheckersPlayer()
game.setNewGame(p1, p2, p3)
game.play();
